package com.shitou.service;

import java.math.BigInteger;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.shitou.dao.IssuesDao;
import com.shitou.pojo.Issues;
import com.shitou.pojo.Milestone;
import com.shitou.pojo.Project;
import com.shitou.pojo.User;
import com.shitou.util.AssignmentUtil;
import com.shitou.util.DateUtil;

@Service
@Transactional
public class IssuesService {

	@Resource
	private IssuesDao issuesDao;
	@Resource
	private NewsService newsService;
	
	public Issues findById(int id) {
		return issuesDao.findById(id);
	}
	
	public List<Issues> findAll() {
		return issuesDao.findAll();
	}
	
	public Integer save(Issues issues, Milestone milestone, User user) {
		issues.setMilestone(milestone);
		issues.setCreatedate(DateUtil.getNow());
		issues.setUser(user);
		issues.setProject(milestone.getProject());
		issuesDao.save(issues);
		newsService.save(issues);
		return issues.getId();
	}
	
	public Integer save(Issues issues,Project project, User user) {
		issues.setCreatedate(DateUtil.getNow());
		issues.setUser(user);
		issues.setProject(project);
		issuesDao.save(issues);
		newsService.save(issues);
		return issues.getId();
	}
	
	public BigInteger findDoneCount(int type, int projectid) {
		return issuesDao.findByTypeAndState(type, AssignmentUtil.CLOSED, projectid);
	}
	
	public BigInteger findUndoneCount(int type, int projectid) {
		return issuesDao.findByTypeAndProjectId(type, projectid).subtract(findDoneCount(type, projectid));
	}
}
